/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <fileOpt.h>
#include <cstring.h>
#include <string.h>
#include <errno.h>

int foptRead(const char *filename, char *outstr, long outsize)
{
    if (!(filename && outstr && outsize > 0))
    {
        printf("file read param error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "r");
    if (!tmpFd)
    {
        printf("file read open error %s, %s\n", filename, filename);
        return -1;
    }
    fseek(tmpFd, 0, SEEK_END);
    long tmpsize = ftell(tmpFd);
    if (tmpsize > outsize)
    {
        printf("file size bigger than out:%s:%ld>%ld\n", filename, tmpsize, outsize);
        tmpsize = outsize;
    }
    fseek(tmpFd, 0, SEEK_SET);
    tmpsize = fread(outstr, 1, tmpsize, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}
int foptWrite(const char *filename, const char *instr, long insize)
{
    if (!(filename && instr && insize > 0))
    {
        printf("file write param error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "w+");
    if (!tmpFd)
    {
        printf("file write open error %s,%s\n", filename, strerror(errno));
        return -1;
    }
    int tmpsize = fwrite(instr, 1, insize, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}

int fileOptReadAll(const char *filename, struct cstring *outstr)
{
    if (!outstr)
    {
        printf("outstr error\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "r");
    long tmpsize;
    if (!tmpFd)
    {
        printf("open error %s\n", filename);
        return -1;
    }
    do
    {
        fseek(tmpFd, 0, SEEK_END);
        tmpsize = ftell(tmpFd);
        if (tmpsize > 10 * 1024 * 1024)
        {
            printf("size too big: %ld\n", tmpsize);
            tmpsize = -1;
            break;
        }
        fseek(tmpFd, 0, SEEK_SET);
        cstrResize(outstr, tmpsize);
        tmpsize = fread(outstr->str, 1, outstr->size, tmpFd);
    } while (0);

    fclose(tmpFd);
    return tmpsize;
}

int fileOptWrite(const char *filename, const struct cstring *instr)
{
    if (!instr)
    {
        printf("outstr error\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "w+");
    if (!tmpFd)
    {
        printf("open error %s\n", filename);
        return -1;
    }
    int tmpsize = fwrite(instr->str, 1, instr->size, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}

int cMakeDir(const char *pathName)
{
    char tmpdir[256] = {0};
    if (!(pathName))
    {
        printf("c make dir error!\n");
        return -1;
    }
    int tmpflag = snprintf(tmpdir, sizeof(tmpdir), "mkdir -p %s", pathName);
    if (tmpflag >= 256)
    {
        printf("error:mkdir -p %s\n", pathName);
        return -1;
    }
    system(tmpdir);
    return 0;
}

int cRemoveFile(const char *fileName)
{
    // 保证当前用户对该文件具有写权限
    // unlink删除文件
    // remove 删除文件或目录
    // system
    return remove(fileName);
}

int foptCreate(const char *filename)
{
    if (!(filename))
    {
        printf("file create name error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "w+");
    if (!tmpFd)
    {
        printf("file write open error %s,%s\n", filename, strerror(errno));
        return -1;
    }
    fclose(tmpFd);
    return 0;
}
// create_file_if_not_exists
int foptCreateIfNotExist(const char *filename)
{
    if (!(filename))
    {
        printf("file create name error!\n");
        return -1;
    }
    FILE *file = fopen(filename, "a"); // 追加模式，文件不存在则创建
    if (file == NULL)
    {
        printf("file write open error %s,%s\n", filename, strerror(errno));
        return -1;
    }
    fclose(file);
    return 0;
}

int fOffsetWrite(const char *filename, long foffset, const char *instr, long insize)
{
    if (!(filename && instr && insize > 0))
    {
        printf("file Offset write param error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "r+");
    if (!tmpFd)
    {
        printf("file Offset write open error %s,%s\n", filename, strerror(errno));
        return -1;
    }
    fseek(tmpFd, foffset, SEEK_SET);
    int tmpsize = fwrite(instr, 1, insize, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}

int fOffsetRead(const char *filename, long foffset, char *outstr, long outsize)
{
    if (!(filename && outstr && outsize > 0))
    {
        printf("file offset read param error!\n");
        return -1;
    }
    FILE *tmpFd = fopen(filename, "r");
    if (!tmpFd)
    {
        printf("file offset read open error %s, %s\n", filename, filename);
        return -1;
    }
    fseek(tmpFd, foffset, SEEK_SET);
    int tmpsize = fread(outstr, 1, outsize, tmpFd);
    fclose(tmpFd);
    return tmpsize;
}

long fGetFileLen(const char *filename)
{
    FILE *tmpFd = fopen(filename, "r");
    if (!tmpFd)
    {
        printf("file read open error %s, %s\n", filename, filename);
        return 0;
    }
    fseek(tmpFd, 0, SEEK_END);
    long tmpsize = ftell(tmpFd);
    fclose(tmpFd);
    return tmpsize;
}